package com.ponxu.mb.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.ponxu.db.SqlBuilder;
import com.ponxu.db.SqlRunner;
import com.ponxu.db.wrap.RowWrapper;
import com.ponxu.db.wrap.impl.IntegerRowWrapper;
import com.ponxu.mb.Global;
import com.ponxu.mb.db.ConnectionManager;
import com.ponxu.mb.model.Comment;
import com.ponxu.mb.model.PageInfo;
import com.ponxu.utils.Lang;

/**
 * @author xwz
 * 
 */
public class CommentService {
	public static String COMMENT_TABLE = Global.read("DB_TABLE_PREFIX")
			+ "comment";
	// 全字段查询
	private static String fullCols = "*";
	private static RowWrapper<Comment> fullRowWrapper = new RowWrapper<Comment>() {
		public Comment wrap(ResultSet rs) throws SQLException {
			Comment comment = new Comment();
			comment.setAuthorEmail(rs.getString("author_email"));
			comment.setAuthorName(rs.getString("author_name"));
			comment.setAuthorUrl(rs.getString("author_url"));
			comment.setCommentTime(rs.getDate("comment_time"));
			comment.setContent(rs.getString("content"));
			comment.setId(rs.getInt("id"));
			comment.setParentId(rs.getInt("parent_id"));
			comment.setPostId(rs.getInt("post_id"));
			comment.setUserId(rs.getInt("user_id"));
			return comment;
		}
	};

	public static Comment getById(int id) {
		SqlBuilder.SELECT(fullCols);
		SqlBuilder.FROM(COMMENT_TABLE);
		SqlBuilder.WHERE("id=?");
		return new SqlRunner(ConnectionManager.get()).selectOne(
				SqlBuilder.SQL(), fullRowWrapper, id);
	}

	public static List<Comment> searchComments(PageInfo page, String where,
			Object... params) {
		SqlRunner runner = new SqlRunner(ConnectionManager.get());

		SqlBuilder.SELECT(fullCols);
		SqlBuilder.FROM(COMMENT_TABLE);
		if (Lang.isNotEmpty(where)) {
			SqlBuilder.WHERE(where);
		}

		// 查询总数
		page.setTotalRow(runner.selectOne(SqlBuilder.SQL_COUNT(),
				IntegerRowWrapper.getInstance(), params));

		SqlBuilder.ORDER_BY("comment_time desc");
		return runner.select(SqlBuilder.SQL() + page.mysqlLimit(),
				fullRowWrapper, params);
	}

	/**
	 * 删除文章评论
	 * 
	 * @param postId
	 * @return
	 */
	public static int clearComments(int[] postIds) {
		SqlBuilder.DELETE_FROM(COMMENT_TABLE);
		SqlBuilder.WHERE("post_id in " + SqlBuilder.inClause(postIds));
		return new SqlRunner(ConnectionManager.get()).execute(SqlBuilder.SQL());
	}

	/**
	 * 查询文章的评论
	 * 
	 * @param pageInfo
	 * @param postId
	 * @return
	 */
	public static List<Comment> searchCommentsByPostId(PageInfo pageInfo,
			int postId) {
		String where = "post_id=?";
		return searchComments(pageInfo, where, postId);
	}

	/**
	 * 评论删除
	 * 
	 * @param ids
	 */
	public static int deleteById(int[] ids) {
		SqlBuilder.DELETE_FROM(COMMENT_TABLE);
		SqlBuilder.WHERE("id in " + SqlBuilder.inClause(ids));
		return new SqlRunner(ConnectionManager.get()).execute(SqlBuilder.SQL());
	}

	public static int add(Comment comment) {
		SqlBuilder.INSERT_INTO(COMMENT_TABLE);
		SqlBuilder.VALUES("post_id,parent_id,content,author_name,"
				+ "author_email,author_url,comment_time,user_id",
				"?,?,?,?,?,?,?,?");

		int id = new SqlRunner(ConnectionManager.get()).insert(
				SqlBuilder.SQL(), comment.getPostId(), comment.getParentId(),
				comment.getContent(), comment.getAuthorName(),
				comment.getAuthorEmail(), comment.getAuthorUrl(),
				comment.getCommentTime(), comment.getUserId());

		comment.setId(id);
		return id;
	}

	public static int update(Comment comment) {
		SqlBuilder.UPDATE(COMMENT_TABLE);
		SqlBuilder.SET("post_id=?,parent_id=?,content=?,author_name=?,"
				+ "author_email=?,author_url=?,comment_time=?,user_id=?");
		SqlBuilder.WHERE("id=?");

		return new SqlRunner(ConnectionManager.get()).execute(SqlBuilder.SQL(),
				comment.getPostId(), comment.getParentId(),
				comment.getContent(), comment.getAuthorName(),
				comment.getAuthorEmail(), comment.getAuthorUrl(),
				comment.getCommentTime(), comment.getUserId(), comment.getId());
	}
}
